<?php

namespace app\api\controller;

use app\BaseController;
use think\exception\HttpResponseException;

class ExtraBase extends BaseController
{
    protected $appConfig = [];
    protected $appVersion = 0;
    protected $isCheck = 0;
    protected $app = '';
    protected $params;
    public function __construct()
    {
        header('content-Type:application/json;charset=utf-8');
        header('Access-Control-Allow-Credentials:true');
        header('Access-Control-Allow-Origin:*');

        $params = input();
        if (empty($params['app_version'])) {
            $params = json_decode(file_get_contents("php://input"), true);
        }
        $this->params = $params;

    }

    /**
     * 返回成功的操作
     * @param mixed $info 消息内容
     * @param mixed $data 返回数据
     * @param mixed $code 返回代码
     */
    public function success($info, $data = [],  $code = 1): void
    {
        $status = 0;
        $result = ['code' => $code, 'info' => $info, 'data' => $data, 'status' => $status];
        throw new HttpResponseException(json($result));
    }

    /**
     * 返回失败的操作
     * @param mixed $info 消息内容
     * @param mixed $data 返回数据
     * @param mixed $code 返回代码
     */
    public function error($info, $data = [], $code = 0): void
    {
        throw new HttpResponseException(json([
            'code' => $code, 'info' => $info, 'data' => $data,
        ]));
    }

}
